package com.photoeditor.demo.util;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.preference.SpUtils;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;

/**
 * Created by @author winn on 18/7/16.
 */

public class PermissionUtil {

    /**
     * 保存当前应用从哪个版本开始升级targetsdkVersion>22
     * TODO 不同应用需要做修改
     */
    public static final int UPGRADE_TARGET_SDK_VERSION_CODE = 3;

    public static final int PERMISSION_REQ_CODE = 4008;

    public static boolean needRequestPermission(String permission) {
        boolean ret = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ret = ContextCompat.checkSelfPermission(ComponentContext.getContext(), permission)
                    != PackageManager.PERMISSION_GRANTED;
        }
        return ret;
    }

    public static boolean requestPermission(Activity activity, int requestCode, String... permission) {
        if (permission == null) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean shouldShowRequestPermissionRationale
                    = checkshouldShowRequestPermissionRationale(permission, activity);
            if (shouldShowRequestPermissionRationale){
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                intent.setData(uri);
                activity.startActivityForResult(intent, PERMISSION_REQ_CODE);
                return false;
            }
            ActivityCompat.requestPermissions(activity, permission,
                    requestCode);
            return true;
        }
        return true;
    }

    public static boolean requestPermission(Fragment fragment, int requestCode, String... permission) {
        if (permission == null) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean shouldShowRequestPermissionRationale =
                    checkshouldShowRequestPermissionRationale(permission, fragment.getActivity());
            if (shouldShowRequestPermissionRationale){
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", fragment.getActivity().getPackageName(), null);
                intent.setData(uri);
                fragment.startActivityForResult(intent, PERMISSION_REQ_CODE);
                return false;
            }
            fragment.requestPermissions(permission, requestCode);
            return true;
        }
        return true;
    }

    public static boolean requestPermissionGroup(Fragment fragment, int requestCode, String[] permission) {
        if (permission == null || fragment == null || fragment.getActivity() == null) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean shouldShowRequestPermissionRationale =
                    checkshouldShowRequestPermissionRationale(permission, fragment.getActivity());
            if (shouldShowRequestPermissionRationale){
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", fragment.getActivity().getPackageName(), null);
                intent.setData(uri);
                fragment.startActivityForResult(intent, PERMISSION_REQ_CODE);
                return false;
            }
            fragment.requestPermissions(permission,
                    requestCode);
            return true;
        }
        return true;
    }

    public static boolean checkshouldShowRequestPermissionRationale(String[] permission, Activity activity) {
        if (activity == null) {
            return false;
        }
        boolean shouldShowRequestPermissionRationale = false;
        for (int i = 0; i < permission.length; ++i) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    permission[i])) {
                shouldShowRequestPermissionRationale = true;
                boolean firstReq = SpUtils.obtain().getBoolean(permission[i], true);
                if (firstReq) {
                    shouldShowRequestPermissionRationale = false;
                    SpUtils.obtain().save(permission[i], false);
                }
                if (shouldShowRequestPermissionRationale) {
                    break;
                }
            }
        }
        return shouldShowRequestPermissionRationale;
    }

    public static void requestOverlayPermission(Fragment fragment) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + fragment.getActivity().getPackageName()));
        fragment.startActivityForResult(intent, PermissionUtil.PERMISSION_REQ_CODE);
    }

    public static void requestOverlayPermission(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        activity.startActivityForResult(intent, PermissionUtil.PERMISSION_REQ_CODE);
    }

//    public static void showRequestFloatWindowDialog(final Activity activity) {
//        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.PERMISSION_RES_DETAIL,
//                StatisticsConstant.PERMISSION_RES_DETAIL_KEY_FLOAT_SHOW, String.valueOf(true));
//        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
//        builder.setTitle(R.string.permission_dialog_title)
//                .setMessage(String.format(activity.getString(R.string.permission_floatwindow_dialog_content),
//                        activity.getString(R.string.app_name)))
//                .setPositiveButton(R.string.permission_dialog_action, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        requestOverlayPermission(activity);
//                        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.PERMISSION_RES_DETAIL,
//                                StatisticsConstant.PERMISSION_RES_DETAIL_KEY_FLOAT_CLICK, String.valueOf(true));
//                        dialog.dismiss();
//                    }
//                })
//                .setOnCancelListener(new DialogInterface.OnCancelListener() {
//                    @Override
//                    public void onCancel(DialogInterface dialogInterface) {
//
//                    }
//                })
//                .create().show();
//    }
}
